﻿@using EIP.Common.Entities.Dtos
@using EIP.Web.AdminLTE.DataUsers.Helpers
@{
    ViewBag.Title = "FieldPermission";
    Layout = null;
}

<section class="content" id="mainDiv">
    <div class="row">
        <div class="col-md-4">
            <div class="box box-primary box-solid">
                <div class="box-header with-border">
                    <h3 class="box-title">模块</h3>
                    <div class="box-tools pull-right">
                        <a href="javascript:void(0);" onclick=" arrowin(); return false; " data-toggle="tooltip" title="展开" class="btn btn-default btn-sm"><i class="fa fa-folder-open" id="arrowin">展开</i></a>
                    </div>
                </div>
                <div class="box-body">
                    @*工具条*@
                    <ul id="fieldTree" class="ztree" style="height: 400px; overflow: auto"></ul>
                </div>
            </div>
        </div>
        <div class="col-md-8">
            <div class="box box-primary box-solid">
                <div class="box-header with-border">
                    <h3 class="box-title">模块字段</h3>
                   
                </div>
                <div class="box-body no-padding">
                    <div style="height: 413px;" class="ScrollBar">
                        @Html.MessageBox(new MessageBoxInput()
            {
                HtmlAttributes = " width='305'",
                MessageBoxType = EnumMessageBoxType.info,
                Msg = "点击左侧菜单查看字段信息"
            })
                        <div id="div_field" style="display: none">
                            @*列表*@
                            <table id="fieldList"></table>
                            <div id="fieldPager"></div>
                            @*列表*@
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</section>
<div class="box-footer text-right">
    <!--以下两种方式提交验证,根据所需选择-->
    <button onclick=" formSubmit(); return false; " type="button" class="btn btn-success" data-btn-type="save" title="确定"><span class="bootstrap-dialog-button-icon fa fa-check"></span>确定</button>
    <button type="button" class="btn btn-danger" data-btn-type="cancel" data-dismiss="modal"><span class="bootstrap-dialog-button-icon fa fa-times"></span>取消</button>
</div>
@Html.Hidden("privilegeMasterValue")
@Html.Hidden("privilegeMaster")
<script>
    $(function () {
        initFieldTreeData();
        initFieldGrid();
        $(".messagebox").show();
        $("#div_field").hide();
    });

    var fieldZNodes, //模块按钮节点
        fieldTreeObj, //模块按钮树对象
        fieldSetting, //功能性树设置
        $fieldgrid; //字段权限列表

    //初始化菜单
    function initFieldMenuTree() {
        //配置
        fieldSetting = {
            view: {
                dblClickExpand: false,
                showLine: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onClick: function (e, treeId, treeNode) {
                    $("#menuname").html(treeNode.name);
                    $(".messagebox").hide();
                    $("#div_field").show();
                    //加载数据
                    getFieldGridData(treeNode);

                }
            }
        };
        fieldTreeObj = $.fn.zTree.init($("#fieldTree"),
            fieldSetting, fieldZNodes);
    }

    //折叠/展开
    var expand = false;

    function arrowin() {
        if (expand) {
            fieldTreeObj.expandAll(false);
            expand = false;
            $("#arrowin").html("展开").attr("class", "fa fa-folder-open");
            $("#arrowin").parent().attr("title", "展开");
        } else {
            fieldTreeObj.expandAll(true);
            expand = true;
            $("#arrowin").html("折叠").attr("class", "fa fa-folder");
            $("#arrowin").parent().attr("title", "折叠");
        }
    }

    //初始化树结构
    function initFieldTreeData() {
        UtilAjaxPost("/System/Permission/GetMenuHavePermissionByPrivilegeMasterValue", {
            privilegeMasterValue: $("#privilegeMasterValue").val(),
            privilegeMaster: $("#privilegeMaster").val(),
            PrivilegeAccess: Language.privilegeAccess.field //字段
        }, function (data) {
            fieldZNodes = data;
            initFieldMenuTree();
        });
    }

    //初始化字段列表
    function initFieldGrid() {
        $fieldgrid = $("#fieldList").jgridview(
        {
            multiselect: true,
            loadonce: false,
            datatype: "json",
            url: "/System/Field/GetFieldByMenuId",
            colModel: [
                { name: "FieldId", hidden: true },
                { label: "显示名称", name: "Label", width: 130, fixed: true },
                //{ label: "字段名称", name: "Name", index: "field.Name", width: 100, fixed: true },
                //{ label: "排序名称", name: "Index", index: "[Index]", width: 100, fixed: true },
                { label: "显示", name: "Hidden", width: 50, fixed: true, formatter: "YesOrNo" },
                { label: "序号", name: "OrderNo", width: 50, fixed: true },
                { label: "备注", name: "Remark", index: "field.Remark", width: 195, fixed: true }
            ],
            height: 343,
            width: 500,
            postData: { IsShowHidden: false },
            sortname: "menu.Name,field.OrderNo",
            sortorder: "asc",
            pager: "fieldPager",
            rowNum: 200,
            rowList: [200, 500, 1000],
            loadComplete: function () {
                var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("fieldTree"));
                var privilegeMenuId = null;
                if (treeNode.length !== 0) {
                    privilegeMenuId = treeNode[0].id;
                    //根据角色获取具有的字段权限信息
                    UtilAjaxPost("/System/Permission/GetPermissionByPrivilegeMasterValue", {
                        privilegeMasterValue: $("#privilegeMasterValue").val(),
                        privilegeMaster: $("#privilegeMaster").val(),
                        PrivilegeAccess: Language.privilegeAccess.field, //字段
                        PrivilegeMenuId: privilegeMenuId
                    }, function (data) {
                        var rowData = $fieldgrid.jqGrid("getRowData");
                        for (var i = 0; i < data.length; i++) {
                            if (rowData.length) {
                                for (var j = 1; j <= rowData.length; j++) {
                                    var fieldId = $fieldgrid.jqGrid("getCell", j, "FieldId");
                                    if (fieldId === data[i].PrivilegeAccessValue) {
                                        GridSetSelection($fieldgrid, j);
                                        break;
                                    }
                                }
                            }
                        }
                    });
                }
            }
        });
    }

    //获取表格数据
    function getFieldGridData(treeNode) {
        GridReloadPagingData($fieldgrid, {
            postData: {
                MenuId: treeNode.id
            }
        });
    }


    //表单提交
    function formSubmit() {
        //获取所有选中行Id
        var json = "";
        var rowData = $fieldgrid.jqGrid("getGridParam", "selarrrow");
        if (rowData.length) {
            for (var i = 0; i < rowData.length; i++) {
                var fieldId = $fieldgrid.jqGrid("getCell", rowData[i], "FieldId"); //name是colModel中的一属性
                json += "{\"P\":\"" + fieldId + "\"},";
            }
        }
        json = json.substring(0, json.length - 1);
        json = "[" + json + "]";
        //获取选中树信息
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("fieldTree"));
        var privilegeMenuId = null;
        if (treeNode.length !== 0) {
            privilegeMenuId = treeNode[0].id;
        }
        UtilAjaxPostWait("/System/Permission/SavePermission", {
            privilegeAccess: Language.privilegeAccess.field, //模块按钮
            privilegeMasterValue: $("#privilegeMasterValue").val(),
            privilegeMaster: $("#privilegeMaster").val(),
            privilegeMenuId: privilegeMenuId,
            menuPermissions: json //权限json字符串
        }, function (data) {
                DialogAjaxResult(data);
        });
    }
</script>